package org.eden.karb.device;

import org.eden.karb.device.audio.AudioConfiguration;
import org.eden.karb.device.audio.impl.AudioMonitorDeviceImpl;
import org.eden.karb.device.audio.impl.AudioRecordDeviceImpl;
import org.jthompson.monsoon.EventMapping;
import org.jthompson.monsoon.factory.CGLibObjectFactory;
import org.jthompson.monsoon.factory.ObjectFactory;

/**
 * Device object factory.
 *
 * @author trsimoes
 */
public class DeviceFactory {

    private static ObjectFactory factory = CGLibObjectFactory.instance();
    private static EventMapping eventMapper = EventMapping.instance();

    /**
     * Gets a device by its type. The returned device is a generated proxy that is capable of sending and/or receiving
     * events.
     *
     * @param clazz device type
     * @param configuration device configuration
     * @return device instance.
     */
    public static Device getDevice(Class clazz, final AudioConfiguration configuration) {

        if (clazz.equals(MonitorDevice.class)) {
            AudioMonitorDeviceImpl device = new AudioMonitorDeviceImpl();
            AudioMonitorDeviceImpl deviceProxy = (AudioMonitorDeviceImpl) factory.generateProxy(device);

            deviceProxy.setKey(device.getKey());
            deviceProxy.setConfiguration(configuration);

            return deviceProxy;
        } else if (clazz.equals(RecordDevice.class)) {
            AudioRecordDeviceImpl device = new AudioRecordDeviceImpl();
            AudioRecordDeviceImpl deviceProxy = (AudioRecordDeviceImpl) factory.generateProxy(device);
            eventMapper.addEventMapping("audioRecord", deviceProxy);

            deviceProxy.setKey(device.getKey());
            deviceProxy.setConfiguration(configuration);

            return deviceProxy;
        }

        return null;
    }
}
